//
//  NovelViewController.m
//  WuLi
//
//  Created by Gollum on 16/9/23.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "NovelViewController.h"
#import "NovelLongHorizontalTableViewCell.h"
#import "NovelHorizontalTableViewCell.h"
#import "AdvertisementTableViewCell.h"

#define kNovelLongHorizontalTableViewCellReuse @"NovelLongHorizontalTableViewCellReuse"
#define kNovelHorizontalTableViewCellReuse @"NovelHorizontalTableViewCellReuse"
#define kAdvertisementTableViewCellReuse @"AdvertisementTableViewCellReuse"

@interface NovelViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;

@end

@implementation NovelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [_tableView registerNib:[UINib nibWithNibName:@"NovelLongHorizontalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kNovelLongHorizontalTableViewCellReuse];
    
    [_tableView registerNib:[UINib nibWithNibName:@"NovelHorizontalTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kNovelHorizontalTableViewCellReuse];
    
    [_tableView registerNib:[UINib nibWithNibName:@"AdvertisementTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kAdvertisementTableViewCellReuse];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        return SCREEN_WIDTH / 2.5 * 0.8 * 2.01;

    }else if(indexPath.section == 1){
        
        return SCREEN_WIDTH / 2 * 2.4;
    }
    else if(indexPath.section == 2){
    
        return SCREEN_WIDTH / 16 *4.5;
    }
        
    else{
    
    
        return 100;
    
    }


}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 30;

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 12;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        NovelLongHorizontalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kNovelLongHorizontalTableViewCellReuse];
//        cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/256.0 green:arc4random_uniform(255)/256.0 blue:arc4random_uniform(255)/256.0 alpha:1];
        return cell;
    }else if (indexPath.section == 1){
        NovelHorizontalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kNovelHorizontalTableViewCellReuse];
        return cell;
    
    }
    else if (indexPath.section == 2){
        AdvertisementTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kAdvertisementTableViewCellReuse];
        return cell;
    }
    else{
        NovelHorizontalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kNovelHorizontalTableViewCellReuse];
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    view.backgroundColor = [UIColor colorWithRed:0.99 green:0.19 blue:0.30 alpha:1.00];
//    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"皇冠"]];
//    imageView.frame = CGRectMake(0, 0, 30, 30);
//    [view addSubview:imageView];
    
    return view;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 100;
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, 0, 0);
    }else if (offsetY >= sectionHeaderHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
